<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<div>
					<label>今日健康分价格：</label>
					<span>
						<el-input v-model="price" placeholder="请输入今日健康分价格"></el-input>
						<el-button @click="handleClick('HEALTH_SCORE_BASE',price)">修改</el-button>
					</span>
				</div>
				<div>
					<label>健康分昨日价格：</label>
					<span>{{ yesterdayPrice }}</span>
				</div>
				<div>
					<label>明日健康分增长：</label>
					<span>
						<el-input v-model="value" placeholder="请输入1-5的数字"></el-input>
						<el-button @click="handleClick('SET_RISE_SCORE_BASE',value)">修改</el-button>
					</span>
				</div>
				<div>
					<label>用户静态收益的10%转换成消费卷</label>
					<el-switch v-model="value1" @change="handleChange"> </el-switch>
				</div>
			</div>
		</el-card>
	</div>
</template>

<script>
	import * as StoreCartApi from "@/api/Search";
	export default {
		data() {
			return {
				yesterdayPrice: 0,
				price: 0,
				value: "",
				value1: false,
				list: [],
			};
		},
		created() {
			this.getInit();
		},
		methods: {
			getInit() {
				StoreCartApi.selectSysVariableList().then((res) => {
					if (res.code === 200) {
						this.list = res.data;
						this.list.forEach((item) => {
							if (item.variableType === "STATIC_INCOME_TO_VOUCHER") {
								if (item.variableValue === "1") {
									this.value1 = true;
								} else {
									this.value1 = false;
								}
							} else if (item.variableType === "HEALTH_SCORE_BASE") {
								// 健康分基数
								this.price = item.variableValue;
							} else if (item.variableType === "YESTERDAY_HEALTH_SCORE_BASE") {
								// 昨日健康分价格
								this.yesterdayPrice = item.variableValue;
							} else if (item.variableType === "SET_RISE_SCORE_BASE") {
								// 明日健康分增长
								this.value = item.variableValue;
							}
						});
					}
				});
			},
			handleChange() {
				let index = this.list.findIndex(
					(item) => item.variableType === "STATIC_INCOME_TO_VOUCHER"
				);
				StoreCartApi.sysVariableMangeUpdate({
					id: this.list[index].id,
					variableType: "STATIC_INCOME_TO_VOUCHER",
					variableValue: this.value1 ? "1" : "0",
				}).then((res) => {
					if (res.code === 200) {
						this.$message({
							message: "修改成功",
							type: "success",
						});
					} else {
						this.$message({
							message: res.message,
							type: "error",
						});
					}
				});
			},
			handleClick(type, value) {
				if (value >= 0 && value <= 5) {
					let index = this.list.findIndex(
						(item) => item.variableType === type
					);
					StoreCartApi.sysVariableMangeUpdate({
						id: this.list[index].id,
						variableType: type,
						variableValue: value,
					}).then((res) => {
						if (res.code === 200) {
							this.$message({
								message: "修改成功",
								type: "success",
							});
						} else {
							this.$message({
								message: res.message,
								type: "error",
							});
						}
					});
				} else {
					this.$message({
						message: "输入的数字需要在0-5之间",
						type: "warning",
					});
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.el-pagination {
		margin-top: 10px;
	}

	.box-card {
		width: 95%;
		margin: auto;
	}

	.Serach_b {
		margin-bottom: 20px;
		line-height: 50px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-textarea {
		margin-left: 20px;
		width: 20%;
	}

	.el-switch {
		margin-left: 10px;
	}
</style>
